package com.zimmur.app.mocolink.manage.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.zimmur.app.mocolink.domain.form.AdminForm;
import com.zimmur.app.mocolink.domain.model.Admin;
import com.zimmur.app.mocolink.domain.model.Role;
import com.zimmur.app.mocolink.domain.param.AdminParam;
import com.zimmur.app.mocolink.domain.util.PageUtil;
import com.zimmur.app.mocolink.domain.util.StringUtils;
import com.zimmur.app.mocolink.domain.vo.AdminInfo;
import com.zimmur.app.mocolink.domain.vo.MenuVo;
import com.zimmur.app.mocolink.domain.vo.RoleInfo;
import com.zimmur.app.mocolink.domain.vo.RoleVo;
import com.zimmur.app.mocolink.manage.web.common.ReturnJsonEntity;
import com.zimmur.app.mocolink.manage.web.common.StatusCodeEnum;
import com.zimmur.app.mocolink.service.IMenuService;
import com.zimmur.app.mocolink.service.IRoleService;

@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {

	@Autowired
	private IRoleService roleService;
	@Autowired
	private IMenuService menuService;
	
	@RequestMapping("/index")
	public String index(ModelMap model){
		List<Role> roleList = roleService.selectAll();
		model.addAttribute("roleList",roleList);
		return "/center/role/list";
	}
	@RequestMapping("/add")
	public String add(ModelMap model){
		List<MenuVo> menuList = menuService.selectMenuWithPermission(0);
		model.addAttribute("role", new Role());
		model.addAttribute("menuList", JSONObject.toJSONString(menuList));
		return "/center/role/edit";
	}
	@RequestMapping("/edit")
	public String edit(ModelMap model){
		Integer roleId = getParam("roleId", 0);
		Role role = roleService.selectById(roleId);
		List<MenuVo> menuList = menuService.selectMenuWithPermission(roleId);
		model.addAttribute("role", role);
		model.addAttribute("menuList", JSONObject.toJSONString(menuList));
		return "/center/role/edit";
	}
	@ResponseBody
	@RequestMapping("/save")
	public ReturnJsonEntity save(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		String name = getParam("roleName");
		Integer roleId = getParam("roleId",0);
		String remark = getParam("remark");
		String menuIds = getParam("menuIds");
		String permissionIds = getParam("permissionIds");
		if(StringUtils.isBlank(name)){
			codeEnum = StatusCodeEnum.CODE100001;
		}else{
			int flag =roleService.saveRoleInfo(roleId, name, remark, menuIds, permissionIds);
			if(flag>0){
				codeEnum = StatusCodeEnum.CODE000000;
			}else if(flag == -1){
				codeEnum = StatusCodeEnum.CODE710403;
			}
		}
		entity.init(codeEnum);
		return entity;
	}
	@ResponseBody
	@RequestMapping("/delete")
	public ReturnJsonEntity delete(){
		ReturnJsonEntity entity = new ReturnJsonEntity();
		StatusCodeEnum codeEnum = StatusCodeEnum.CODE100000;
		Integer roleId = getParam("roleId", 0);
		int flag =roleService.deleteById(roleId);
		if(flag>0){
			codeEnum = StatusCodeEnum.CODE000000;
		}else if(flag == -1){
			codeEnum = StatusCodeEnum.CODE710401;
		}
		entity.init(codeEnum);
		return entity;
	}
}
